//
//  ProductShowController.m
//  meituanAddressManagee
//
//  Created by WuXushun on 16/1/27.
//  Copyright © 2016年 wuxushun. All rights reserved.
//

#define statusbarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define navigationbarHeight self.navigationController.navigationBar.frame.size.height

#import "ProductShowController.h"
#import "ProductShowLeftCell.h"
#import "ProductShowRightCell.h"
#import "HttpRequest.h"
#import "ProductLeftModel.h"

@interface ProductShowController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) IBOutlet UITableView *leftTableview;
@property (strong, nonatomic) IBOutlet UITableView *rightTableview;

@property (strong, nonatomic) NSArray *leftTempArray;
@property (weak, nonatomic) ProductLeftModel *tempLeftModel;

@property (strong, nonatomic) NSMutableArray *rightTempArray;

@end

@implementation ProductShowController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self leftHttpRequest];
    
}

-(void)leftHttpRequest{

    [HttpRequest GetCategory:^(BOOL isSuccess, NSArray *resultArray, NSError *error) {
        
        if (isSuccess) {
            
            self.leftTempArray = resultArray;
            [self.leftTableview reloadData];
            
            for (ProductLeftModel *obj in self.leftTempArray) {
                
                [self rightHtppRequestWithId:obj.presentID];
                
            }
            
        }
        
    } withPath:@"Mall/GetCategory"];

}

-(void)rightHtppRequestWithId:(NSString *)ID{
    NSLog(@"%@",ID);
    [HttpRequest GetGoodsListByCategory:^(BOOL isSuccess, NSArray *resultArray, NSError *error) {
        
//        if (isSuccess) {
//            
//            if (self.rightTempArray == nil) {
//                
//                self.rightTempArray = [NSMutableArray new];
//                
//            }
//            
//            for (id obj in resultArray) {
//                
//                [self.rightTempArray addObject:obj];
//                
//            }
//            
//        }
        
    } withPath:@"Mall/GetGoodsListByCategory"
                                  start:@"0"
                                  limit:@"90"
                                   type:ID
                                user_id:@"789"
                       ExtractingShopId:@""];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.leftTableview) {
        
        return 50;
        
    }else{
    
        return ([UIScreen mainScreen].bounds.size.width - 80) * 280 / 570;
    
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (tableView == self.leftTableview) {
        
        return 0.1;
        
    }else{
        
        if (section == 0) {
            
            return statusbarHeight + navigationbarHeight;
            
        }else{
        
            return 0.1;
        
        }
        
    }
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 0.1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (tableView == self.leftTableview) {
        
        return self.leftTempArray.count;
        
    }else{
        
        return 10;
        
    }

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    if (tableView == self.leftTableview) {
        
        return 1;
        
    }else{
        
        return 1;
        
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.leftTableview) {
        
        static NSString *reCell = @"ProductShowLeftCell";
        
        ProductShowLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:reCell];
        
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ProductShowLeftCell" owner:self options:nil]lastObject];
            
            
            
        }
        
        cell.contentView.backgroundColor = [UIColor colorWithRed:238.f / 255.f
                                                     green:238.f / 255.f
                                                      blue:238.f / 255.f
                                                     alpha:1.f];
        
        self.tempLeftModel = [self.leftTempArray objectAtIndex:indexPath.row];
        
        cell.label.attributedText = [self setLeftTitleStyle:[NSString stringWithFormat:@"%@\n%@",self.tempLeftModel.titile,self.tempLeftModel.subtitle]
                                                  withColor:[UIColor blackColor]];
        
        return cell;
        
    }else{
        
        static NSString *reCell = @"ProductShowRightCell";
        
        ProductShowRightCell *cell = [tableView dequeueReusableCellWithIdentifier:reCell];
        
        if (cell == nil) {
            
            cell = [[[NSBundle mainBundle] loadNibNamed:@"ProductShowRightCell" owner:self options:nil]lastObject];
            
        }
        
        
        return cell;
        
    }

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{



}

-(NSAttributedString *)setLeftTitleStyle:(NSString *)string withColor:(UIColor *)color{

    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    [style setAlignment:NSTextAlignmentCenter];
    style.lineSpacing = 0.f;
    style.paragraphSpacing = 3.f;
    
    NSString *tempStr = [[string componentsSeparatedByString:@"\n"] objectAtIndex:0];
    
    NSMutableAttributedString *tempString = [[NSMutableAttributedString alloc]initWithString:string];
    
    [tempString addAttribute:NSParagraphStyleAttributeName
                       value:style
                       range:NSMakeRange(0, string.length)];
    
    [tempString addAttribute:NSForegroundColorAttributeName
                       value:color
                       range:NSMakeRange(0, tempStr.length)];
    
    [tempString addAttribute:NSFontAttributeName
                       value:[UIFont systemFontOfSize:14.f]
                       range:NSMakeRange(0, tempStr.length)];
    
    [tempString addAttribute:NSFontAttributeName
                       value:[UIFont systemFontOfSize:11.f]
                       range:NSMakeRange(tempStr.length, string.length - tempStr.length)];
    
    [tempString addAttribute:NSForegroundColorAttributeName
                       value:[UIColor colorWithRed:138.f / 255.f
                                             green:138.f / 255.f
                                              blue:138.f / 255.f
                                             alpha:1.f]
                       range:NSMakeRange(tempStr.length, string.length - tempStr.length)];
    
    return tempString;
}

@end
